import { FAILED_ITEM } from "./formulaSheet.js";

// 获取失败产物
export const getFailureProduce = (type = "common") => {
    const summary = FAILED_ITEM[type];
    return getThing("armor", summary._itemId);
};

export const getThing = (type, id) => {
    if (type === "item") return getItems(id);
    if (type === "armor") return getArmor(id);
    if (type === "weapon") return getWeapon(id);
};

export const getItems = (id) => {
    return $dataItems.find((item) => item && item.id === id);
};

export const getArmor = (id) => {
    return $dataArmors.find((item) => item && item.id === id);
};

export const getWeapon = (id) => {
    return $dataWeapons.find((item) => item && item.id === id);
};

export const getEnemy = (id) => {
    return $dataEnemies.find((item) => item && item.id === id);
};

export const getEnemyByArmorEnemyId = (enemyId) => {
    return getEnemy(Number(enemyId.trim()));
};

export const getMetaAttr = (
    meta,
    key,
    { defaultValue = "", formatter = (v) => v }
) => {
    if (!meta) return defaultValue;
    const value = meta[key] ? meta[key].trim() : defaultValue;
    return formatter(value);
};

export const isQiwu = (item) => {
    //  规则： item（id:1~119)、weapon(id:1-24)、armor(id:1~137)
    const isRange = (data, [min, max]) => data >= min && data <= max;
    const dataClass = item._dataClass;
    const itemId = item._itemId;
    const condition1 = dataClass === "item" && isRange(itemId, [1, 119]);
    const condition2 = dataClass === "weapon" && isRange(itemId, [1, 24]);
    const condition3 = dataClass === "armor" && isRange(itemId, [1, 137]);

    return condition1 || condition2 || condition3;
};

const getFailureProduceTypeByItems = (leftItem, rightItem) => {
    return "common";
};

export const getFailureProduceByItems = (leftItem, rightItem) => {
    const type = getFailureProduceTypeByItems(leftItem, rightItem);
    return getFailureProduce(type);
};

export const getCatchLevelByItemMeta = (meta) => {
    if (meta && meta["Catch Level"]) return Number(meta["Catch Level"].trim());
    if (meta["Enemy Id"]) {
        const enemy = getEnemyByArmorEnemyId(meta["Enemy Id"]);
        return Number(enemy.meta["Catch Level"].trim());
    }
};